# -*- coding: utf-8 -*-
import scrapy
from ..items import WallpaperItem
import re

class WallpaperSpider(scrapy.Spider):
    name = 'Wallpaper'
    # allowed_domains = ['ivsky.com']
    start_urls = ['http://ivsky.com/bizhi/']

    def parse(self, response):
        sort_urls = response.xpath("/html/body/div[3]/div[2]/ul/li/a/@href")
        for i in sort_urls:
            sort_url = i.get()
            if sort_url != '/bizhi/':
                yield response.follow(sort_url,callback=self.then)

    def then(self,response):
        sort_next_page = response.xpath('/html/body/div[3]/div[4]/div[4]/a[last()]/@href')
        if sort_next_page:
            sort_next_page = sort_next_page.get()
            yield response.follow(sort_next_page,callback=self.then)
        set_urls = response.xpath('/html/body/div[3]/div[4]/ul/li/p/a/@href')
        for set_url in set_urls:
            set_url = set_url.get()
            yield response.follow(set_url,callback=self.after)

    def after(self,response):
        images_next_page = response.xpath('/html/body/div[3]/div[4]/div[4]/div/a[2]/@href')
        if images_next_page:
            images_next_page = images_next_page.get()
            yield response.follow(images_next_page,callback=self.after)
        images_addr = response.xpath('/html/body/div[3]/div[4]/ul/li/p/a/@href')
        for image_addr in images_addr:
            image_addr = image_addr.get()
            yield response.follow(image_addr,callback=self.later)

    def later(self,response):
        item = WallpaperItem()

        file_path = response.xpath('/html/body/div[3]/div[1]/a/text()')
        path = "full/"+file_path[2].get()+'/'+file_path[3].get()+"/"
        img_path = ''
        for i in re.split(r'\s+',path):
            img_path += i
        item['img_path'] = img_path

        name = response.xpath('/html/body/div[3]/div[3]/div[1]/h1/text()')
        img_name = ''
        for i in re.split(r'\s+',name.get()):
            img_name += i
        item['img_name'] = img_name

        url = response.xpath('//*[@id="imgis"]/@src').get()
        img_url = 'http://img.ivsky.com/img/bizhi/pic'+url[34:]
        item['img_url'] = img_url

        yield item